package com.bite.springbook.controller;

import com.bite.springbook.model.Result;
import com.bite.springbook.service.UserService;
import com.bite.springbook.constant.Constants;
import com.bite.springbook.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/login" , produces = "application/json")
    public Result login(String userName , String password , HttpSession session){
        log.info("用户登入 userName:{} , password:{}" , userName , password);
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return Result.fail("用户不能为空");
        }

        //根据用户名查询用户信息
        UserInfo userInfo = userService.queryUserByName(userName);
        if(userInfo == null || userInfo.getId() < 0){
            return Result.fail("用户不存在");
        }
        if(!password.equals(userInfo.getPassword())){

            return Result.fail("用户不存在");
        }

        userInfo.setPassword("");
        session.setAttribute(Constants.USER_SESSION_KEY , userInfo);
        return Result.success("");
    }
}
